<?php
/**
 * 加盟商
 */
defined('InShopNC') or exit('Access Invalid!');
class franchiserModel extends Model{
    /**
     * 江湖结盟文章分类id
     * @var int
     */
    protected $_articleClassId=15;
    public function __construct() {
        parent::__construct('franchiser');
    }

    /**
     * 查询所有加盟商
     * @param array $condition
     * @param array $fields
     * @param  Page $page
     * @return array|null
     */
    public function getShowList(array $condition=array(),$fields=array('franchisee_id','franchisee_name','franchisee_address','linkname','linkname_mobile','areaid_1','areaid_2','article_id'), $page=null){
        $this->field($fields);
        if($page instanceof Page){
            $pageSize=$page->getEachNum();
            $pageStart=$page->getNowPage()>1?$page->getNowPage():1;
            $limit=($pageStart-1)*$pageSize.','.$pageSize;
        }else{
            $limit=false;
        }
        return $this->table('franchiser')->where($condition)->limit($limit)->select();
    }
    /**
     * 列表
     *
     * @param array $condition 检索条件
     * @param obj $page 分页
     * @return array 数组结构的返回结果
     */
    public function getFranchiseList($condition,$page=''){
        $condition_str = $this->_condition($condition);
        $param = array();
        $param['table'] = 'article';
        $param['where'] = $condition_str;
        $param['limit'] = $condition['limit'];
        $param['order']	= (empty($condition['order'])?'article_sort asc,article_time desc':$condition['order']);
        $result = Db::select($param,$page);
        return $result;
    }

    /**
     *
     * @param $id
     * @return mixed
     */
    public function getFranchise($id){
        return $this->table('franchiser')->where(array('franchisee_id'=>$id))->find();
    }
    /**
     * 获取加盟商文章列表
     * @return array|null
     */
    public function getArticleList(){
        $sql='SELECT `c`.`franchisee_name`,`a`.`article_id` ,`b`.`file_name` FROM `%TALBE_PREFIX%franchiser` `c` LEFT JOIN `%TALBE_PREFIX%article` `a`ON `c`.`article_id` =`a`.`article_id` LEFT JOIN `%TALBE_PREFIX%upload` `b` ON `a`.`article_id`=`b`.`item_id` WHERE `a`.`article_thumb`>1 AND `a`.`article_show`=1 AND `b`.`upload_type`=1 ORDER BY `a`.`article_sort` ASC';
        return $this->query(str_replace('%TALBE_PREFIX%',$this->table_prefix,$sql));
    }

    /**
     * 添加加盟店申请信息
     * @param $data
     * @return mixed
     */
    public function addApply($data){
        return $this->table('franchiser_apply')->insert($data);
    }
}